package md.mru.frontend.beans;

import md.mru.backend.core.dtos.*;
import org.primefaces.context.RequestContext;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 29.03.2014
 */
@ManagedBean(name = "additionalDataBean")
@SessionScoped
public class AdditionalDataBean extends BackingBean {

    private List<CitizenDto> citizens = new ArrayList<>();
    private List<CitizenshipDto> citizenShips = new ArrayList<>();
    private List<DocumentDto> documents = new ArrayList<>();
    private List<DocumentTypeDto> documentTypes = new ArrayList<>();
    private List<FamilyMemberDto> familyMembers = new ArrayList<>();
    private List<FamilyRelationDto> familyRelations = new ArrayList<>();

    public void deleteCitizen(CitizenDto c) {
        this.citizens.remove(c);
    }

    public void addNewCitizen(String editRowId) {
        CitizenDto c = new CitizenDto();
        citizens.add(c);
        RequestContext.getCurrentInstance().execute(super.constructOnCompleteScript("cz-formId", "czData", editRowId));
    }

    public void deleteDocument(DocumentDto d) {
        this.documents.remove(d);
    }

    public void addNewDocument(String editRowId) {
        DocumentDto d = new DocumentDto();
        documents.add(d);
        RequestContext.getCurrentInstance().execute(super.constructOnCompleteScript("doc-formId", "docData", editRowId));
    }

    public void deleteFamilyMember(FamilyMemberDto fm) {
        this.familyMembers.remove(fm);
    }

    public void addNewFamilyMember(String editRowId) {
        FamilyMemberDto fm = new FamilyMemberDto();
        familyMembers.add(fm);
        RequestContext.getCurrentInstance().execute(super.constructOnCompleteScript("fm-formId", "fmData", editRowId));
    }

    public List<CitizenDto> getCitizens() {
        if (citizens == null) {
            citizens = new ArrayList<>(0);
        }
        return citizens;
    }

    public void setCitizens(List<CitizenDto> citizens) {
        this.citizens = citizens;
    }

    public List<DocumentDto> getDocuments() {
        return documents;
    }

    public void setDocuments(List<DocumentDto> documents) {
        this.documents = documents;
    }

    public List<DocumentTypeDto> getDocumentTypes() {
        if (documentTypes.isEmpty()) {
            documentTypes = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                DocumentTypeDto d = new DocumentTypeDto();
                d.setId(i);
                d.setName("Documetent_type_" + i);
                documentTypes.add(d);
                getObjectMappingConverter().mapObjectList(documentTypes);
            }

        }
        return documentTypes;
    }

    public void setDocumentTypes(List<DocumentTypeDto> documentTypes) {
        this.documentTypes = documentTypes;
    }

    public List<CitizenshipDto> getCitizenShips() {
        if (citizenShips.isEmpty()) {
            citizenShips = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                CitizenshipDto c = new CitizenshipDto();
                c.setId(i);
                c.setName("Citizen_" + i);
                citizenShips.add(c);
                getObjectMappingConverter().mapObjectList(citizenShips);
            }

        }
        return citizenShips;
    }

    public void setCitizenShips(List<CitizenshipDto> citizenShips) {
        this.citizenShips = citizenShips;
    }

    public List<FamilyMemberDto> getFamilyMembers() {
        return familyMembers;
    }

    public void setFamilyMembers(List<FamilyMemberDto> familyMembers) {
        this.familyMembers = familyMembers;
    }

    public List<FamilyRelationDto> getFamilyRelations() {
        if (familyRelations.isEmpty()) {
            familyRelations = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                FamilyRelationDto fr = new FamilyRelationDto();
                fr.setId(i);
                fr.setName("Family Relation_" + i);
                familyRelations.add(fr);
                getObjectMappingConverter().mapObjectList(familyRelations);
            }

        }
        return familyRelations;
    }

    public void setFamilyRelations(List<FamilyRelationDto> familyRelations) {
        this.familyRelations = familyRelations;
    }
}
